﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;

namespace ManagementSystem
{
    /// <summary>
    /// 查询学生信息窗体
    /// </summary>
    public partial class SelectStudent : Form
    {
        private SystemServices services;
        public SelectStudent()
        {
            InitializeComponent();
            services = new SystemServices();
        }

        /// <summary>
        /// 执行按学号查询操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonID_Click(object sender, EventArgs e)
        {
            //获取输入
            string ID = textBoxID.Text.Trim();
            if (string.IsNullOrEmpty(ID))
            {
                MessageBox.Show("学号不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            //异常处理
            try
            {
                // 获取查询信息
                var filtereds = services.SelectsID(ID).ToList();

                if (!filtereds.Any())
                {
                    MessageBox.Show("未找到匹配的学生信息", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                //显示查询结果窗体
                FormStudent formstudent1 = new FormStudent(filtereds);
                formstudent1.ShowDialog();
                
               
            }
            catch (Exception ex)
            {
                MessageBox.Show($"查询过程中发生错误: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        

        /// <summary>
        /// 执行按姓名查询操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonName_Click(object sender, EventArgs e)
        {
            //获取输入
            string Name = textBoxName.Text.Trim();
            if (string.IsNullOrEmpty(Name))
            {
                MessageBox.Show("姓名不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            //异常处理
            try
            {
                // 获取查询信息
                var filtereds = services.SelectsName(Name).ToList();

                if (!filtereds.Any())
                {
                    MessageBox.Show("未找到匹配的学生信息", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                //显示查询结果窗体
                FormStudent2 formstudent2 = new FormStudent2(filtereds);
                formstudent2.ShowDialog();


            }
            catch (Exception ex)
            {
                MessageBox.Show($"查询过程中发生错误: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// 执行按班级查询操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonClass_Click(object sender, EventArgs e)
        {
            //获取输入
            string Class = textBoxClass.Text.Trim();
            if (string.IsNullOrEmpty(Class))
            {
                MessageBox.Show("班级不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            //异常处理
            try
            {
                // 获取查询信息
                var filtereds = services.SelectsClass(Class).ToList();

                if (!filtereds.Any())
                {
                    MessageBox.Show("未找到匹配的学生信息", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                //显示查询结果窗体
                FormStudent3 formstudent3 = new FormStudent3(filtereds);
                formstudent3.ShowDialog();


            }
            catch (Exception ex)
            {
                MessageBox.Show($"查询过程中发生错误: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// 执行按籍贯查询操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonHometown_Click(object sender, EventArgs e)
        {
            //获取输入
            string Hometown = textBoxHometown.Text.Trim();
            if (string.IsNullOrEmpty(Hometown))
            {
                MessageBox.Show("籍贯不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            //异常处理
            try
            {
                // 获取查询信息
                var filtereds = services.SelectsHometown(Hometown).ToList();

                if (!filtereds.Any())
                {
                    MessageBox.Show("未找到匹配的学生信息", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                //显示查询结果窗体
                FormStudent4 formstudent4 = new FormStudent4(filtereds);
                formstudent4.ShowDialog();


            }
            catch (Exception ex)
            {
                MessageBox.Show($"查询过程中发生错误: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        private void SelectStudent_Load(object sender, EventArgs e)
        {

        }
    }
}
